/**
 * 分区管理相关类型定义
 */

/**
 * 社区进入状态
 */
export interface CommunityEntryState {
  isFirstEntry: boolean
  hasExistingZones: boolean
  zoneCount: number
  lastModifiedTime?: string
}

/**
 * 分区信息
 */
export interface Zone {
  id?: number
  zoneName: string
  zoneCode: string
  zoneType: string
  zoneColor: string
  zoneDescription?: string
  communityId: number
  propertyCompanyId: number
  zoneBoundary?: string
  zoneCenterX?: number
  zoneCenterY?: number
  zoneArea?: number
  status: number
  deleted: number
  createTime?: string
  updateTime?: string
  createBy?: number
  updateBy?: number
}

/**
 * 楼栋信息
 */
export interface Building {
  id: number
  buildingName: string
  buildingType: string
  buildingTypeName?: string
  communityId: number
  status: 'available' | 'used' | 'selected'
  zoneId?: number
  positionX?: number
  positionY?: number
}

/**
 * 楼栋位置数据
 */
export interface BuildingPositionData {
  id: number
  zoneId: number
  positionX: number
  positionY: number
}

/**
 * 分区绘图数据
 */
export interface ZoneDrawingData {
  id?: number
  communityId: number
  propertyCompanyId?: number
  drawingData: string
  buildings: BuildingPositionData[]
  zones: ZoneData[]
  canvasWidth?: number
  canvasHeight?: number
  zoomLevel?: number
  version: number
  isCurrent: boolean
  createTime?: string
  updateTime?: string
}

/**
 * 分区数据（画布格式）
 */
export interface ZoneData {
  id?: number
  name: string
  color: string
  points: Point[]
  buildings: number[]
}

/**
 * 坐标点
 */
export interface Point {
  x: number
  y: number
}

/**
 * 分区保存数据
 */
export interface ZoneSaveData {
  communityId: number
  propertyCompanyId: number
  zones: ZoneData[]
  buildings: BuildingPositionData[]
  drawingData: ZoneDrawingData
  operationType: 'CREATE' | 'UPDATE'
}

/**
 * 分区管理初始数据
 */
export interface ZoneManagementInitialData {
  communityInfo: CommunityInfo
  buildingList: Building[]
  zoneList: Zone[]
  drawingData: ZoneDrawingData | null
}

/**
 * 社区信息
 */
export interface CommunityInfo {
  id: number
  communityName: string
  communityAddress: string
  propertyCompanyId: number
  propertyCompanyName?: string
  status: number
  createTime?: string
  updateTime?: string
}

/**
 * 加载状态
 */
export interface LoadingState {
  isLoading: boolean
  progress: number
  message: string
  stage: 'init' | 'community' | 'buildings' | 'zones' | 'drawing' | 'complete'
}

/**
 * 分区信息更新DTO
 */
export interface ZoneInfoUpdateDTO {
  zoneName: string
  zoneCode: string
  zoneType: string
  zoneColor: string
  zoneDescription?: string
}

/**
 * 分区绘图更新DTO
 */
export interface ZoneDrawingUpdateDTO {
  zoneBoundary: string
  zoneCenterX: number
  zoneCenterY: number
  zoneArea: number
  drawingData: string
}

/**
 * 分区统计信息
 */
export interface ZoneStatistics {
  zoneId: number
  zoneName: string
  buildingCount: number
  householdCount: number
  residentCount: number
  vehicleCount: number
  parkingCount: number
  propertyFeeAmount: number
  complaintCount: number
  repairCount: number
  deviceCount: number
  statisticsDate: string
}

/**
 * 分区统计趋势
 */
export interface ZoneStatisticsTrend {
  date: string
  buildingCount: number
  householdCount: number
  residentCount: number
  vehicleCount: number
  parkingCount: number
  propertyFeeAmount: number
  complaintCount: number
  repairCount: number
  deviceCount: number
}

/**
 * 分区统计对比
 */
export interface ZoneStatisticsComparison {
  zoneId: number
  zoneName: string
  buildingCount: number
  householdCount: number
  residentCount: number
  vehicleCount: number
  parkingCount: number
  propertyFeeAmount: number
  complaintCount: number
  repairCount: number
  deviceCount: number
  ranking: number
}

/**
 * 分区综合统计报告
 */
export interface ZoneComprehensiveReport {
  zoneId: number
  zoneName: string
  overview: ZoneStatistics
  trends: ZoneStatisticsTrend[]
  comparison: ZoneStatisticsComparison[]
  recommendations: string[]
  reportDate: string
}

/**
 * 协作会话信息
 */
export interface CollaborationSession {
  id: string
  communityId: number
  zoneId?: number
  userId: number
  userName: string
  userAvatar?: string
  joinTime: string
  lastActiveTime: string
  isOnline: boolean
}

/**
 * 聊天消息
 */
export interface ChatMessage {
  id: string
  sessionId: string
  userId: number
  userName: string
  userAvatar?: string
  message: string
  messageType: 'text' | 'image' | 'file'
  sendTime: string
  status: 'pending' | 'sent' | 'delivered' | 'read' | 'failed'
}

/**
 * 分区拖拽操作记录
 */
export interface ZoneDragHistory {
  id: number
  zoneId: number
  userId: number
  userName: string
  operationType: 'drag' | 'resize' | 'move'
  oldData: string
  newData: string
  operationTime: string
  ipAddress?: string
}

/**
 * 楼栋位置判断结果
 */
export interface ZoneLocationResult {
  zoneId: number | null
  zoneName: string
  isInside: boolean
  distance: number
}

/**
 * 默认框生成建议
 */
export interface DefaultZoneSuggestion {
  x: number
  y: number
  width: number
  height: number
  confidence: number
  reason: string
}

/**
 * 图形数据
 */
export interface ShapeData {
  shapeType: 'POLYGON' | 'CURVE' | 'RECTANGLE'
  points: Point[]
  style: {
    fillColor: string
    strokeColor: string
    strokeWidth: number
    opacity: number
  }
  metadata?: {
    createdAt: string
    updatedAt: string
    version: number
  }
}

/**
 * 拖拽手柄
 */
export interface DragHandle {
  type: 'vertex' | 'edge' | 'center'
  index: number
  point: Point
}

/**
 * 分区形状基类
 */
export interface ZoneShape {
  id: number
  name: string
  color: string
  points: Point[]
  selected: boolean
  draw: (ctx: CanvasRenderingContext2D) => void
  findDragHandle: (point: Point) => DragHandle | null
  updatePoint: (index: number, newPoint: Point) => void
  containsPoint: (point: Point) => boolean
}

/**
 * 事件类型
 */
export const ZoneManagementEvents = {
  BUILDING_DROPPED: 'building-dropped',
  BUILDING_DELETED: 'building-deleted',
  ZONE_CREATED: 'zone-created',
  ZONE_UPDATED: 'zone-updated',
  ZONE_DELETED: 'zone-deleted',
  SAVE_REQUESTED: 'save-requested',
  DATA_LOADED: 'data-loaded',
  ENTRY_STATE_DETERMINED: 'entry-state-determined'
} as const

export type ZoneManagementEventType = typeof ZoneManagementEvents[keyof typeof ZoneManagementEvents]
